package com.zengde.ai_sandbox.test.testthread.casnio;

import lombok.Data;

@Data
public class Casnioer {

    private int balance = 0;
    private StringBuffer stringBuffer ;

    public synchronized void earnMoney(int money) {
        balance += money;
        println(1,money);
    }

    public synchronized void spendMoney(int money) {
        balance -= money;
        println(2,money);
    }

    private void println(int type, int money) {
        stringBuffer = new StringBuffer();
        if (type == 1) {
            stringBuffer.append("恭喜 ")
                    .append(Thread.currentThread().getName())
                    .append(" 赢了 ")
                    .append(money + " 元钱")
                    .append(" 当前余额为 " + balance + " 元 " )
                    .append("\n");
        } else {
            stringBuffer
                    .append(Thread.currentThread().getName())
                    .append(" 输了 ")
                    .append(money + " 元钱，不要气馁！")
                    .append(" 当前余额为 " + balance + " 元")
                    .append("\n");

        }
        System.out.println(stringBuffer.toString());
    }

}
